//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    cur:0,
    currentSection: app.globalData.sections[0],
    uprightTime:0,
    againstTime:0,
    uprightBtnText:"正方发言",
    againstBtnText:"反方发言",

  },
  rediu: 64,
  lineW: 10,
 //生命周期
  onShow:function(){
    var self = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        self.rediu = 150 * res.windowWidth / 375 / 2
        // self.lineW = 20 * res.windowWidth / 375 / 2
        console.log(self.rediu)
      },
    })


    console.log("onShow")
    console.log(app.globalData.sections)
    this.setData({
      currentSection: app.globalData.sections[this.data.cur],
      uprightTime: app.globalData.sections[this.data.cur].totalTime,
      againstTime: app.globalData.sections[this.data.cur].totalTime
    })




    // var x = 300rpx / 
    var upCtx = wx.createCanvasContext("uprightId", this)
    upCtx.arc(this.rediu, this.rediu, this.rediu - this.lineW/2, -Math.PI / 2,Math.PI/2,false)

    upCtx.setStrokeStyle('#0099ff')
    upCtx.setLineWidth(this.lineW)
    upCtx.stroke()
    // context.set
    upCtx.draw()

    var agCtx = wx.createCanvasContext("againstId", this)
    agCtx.arc(this.rediu, this.rediu, this.rediu-this.lineW/2, -Math.PI / 2, Math.PI / 2, false)
    agCtx.setStrokeStyle('#0099ff')
    agCtx.setLineWidth(this.lineW)
    agCtx.stroke()
    // context.set
    agCtx.draw()
    

  },
  onHide:function(){
    console.log("onHide")
    //需要暂停计时器,只要离开了这个就暂停计时器
    this.stopAnimation()
  },

  stopAnimation:function(){


    //  看看反方是否在计时间
    if (this.againstTimer != null) {
      clearInterval(this.againstTimer)
      this.againstTimer = null
      this.setData({
        againstBtnText: "反方发言"
      })
    }
    //看看正方是否在计时间
    if (this.uprightTimer != null) {
      clearInterval(this.uprightTimer)
      this.uprightTimer = null
      this.setData({
        uprightBtnText: "正方发言"
      })
    }
  },

  // 切换阶段点击事件
  onSectionClicked :function(){
    var self = this;
    var arr = []
    console.log(app.globalData.sections)
    for (var i = 0; i < app.globalData.sections.length;i++){
      arr.push(app.globalData.sections[i].name)
    }

    wx.showActionSheet({
      itemList: arr,
      success: function (res) {
        self.stopAnimation()
        self.setData({
          cur: res.tapIndex,
          currentSection: app.globalData.sections[res.tapIndex],
          uprightTime: app.globalData.sections[res.tapIndex].totalTime,
          againstTime: app.globalData.sections[res.tapIndex].totalTime
        });

      }
    });

  },

 //开始的事件
  uprightAction:function(event){

  //  看看反方是否在计时间
    if (this.againstTimer != null) {
      clearInterval(this.againstTimer)
        this.againstTimer = null
        this.setData({
          againstBtnText: "反方发言"
      })
    }
    //正方的逻辑
    if (this.uprightTimer == null) {
      this.uprightTimer = setInterval(this.uprightTimerAnmation, 1000 );
      this.setData({
        uprightBtnText:"停止发言"
      })
    } else {
      clearInterval(this.uprightTimer)
      this.uprightTimer = null
      this.setData({
        uprightBtnText: "正方发言"
      })
    }

  },
  upRightRadiu:0,
  uprightTimerAnmation:function(){
    
    if (this.data.uprightTime <=0){
      clearInterval(this.uprightTimer)
      this.uprightTimer = null
      return;
    }
    this.setData({
      uprightTime: this.data.uprightTime -1
    })

    // var x = 300rpx / 
    this.upRightRadiu = this.upRightRadiu + Math.PI / 4

    var off = this.upRightRadiu %(Math.PI*2);

    var end = Math.PI / 2 + off
    var start = end - Math.PI;
    var upCtx = wx.createCanvasContext("uprightId", this)
    upCtx.arc(this.rediu, this.rediu, this.rediu-this.lineW/2, start, end, false)

    upCtx.setStrokeStyle('#0099ff')
    upCtx.setLineWidth(this.lineW)
    upCtx.stroke()
    // context.set
    upCtx.draw()
  },


  againstAction: function (event) {
    //看看正方是否在计时间
    if (this.uprightTimer != null) {
      clearInterval(this.uprightTimer)
        this.uprightTimer = null
        this.setData({
        uprightBtnText: "正方发言"
      })
    }


    //反方的逻辑
    if (this.againstTimer == null) {
      this.againstTimer = setInterval(this.againstTimerAnmation, 1000);
      this.setData({
        againstBtnText: "停止发言"
      })
    } else {
      clearInterval(this.againstTimer)
      this.againstTimer = null
      this.setData({
        againstBtnText: "正方发言"
      })
    }

  },

  againstRadiu: 0,
  againstTimerAnmation: function () {

    if (this.data.againstTime <= 0) {
      clearInterval(this.againstTimer)
      this.againstTimer = null
      return;
    }
    this.setData({
      againstTime: this.data.againstTime - 1
    })

    // var x = 300rpx / 
    this.againstRadiu = this.againstRadiu + Math.PI / 4

    var off = this.againstRadiu % (Math.PI * 2);

    var end = Math.PI / 2 + off
    var start = end - Math.PI;
    var upCtx = wx.createCanvasContext("againstId", this)
    upCtx.arc(this.rediu, this.rediu, this.rediu-this.lineW/2, start, end, false)

    upCtx.setStrokeStyle('#0099ff')
    upCtx.setLineWidth(this.lineW)
    upCtx.stroke()
    // context.set
    upCtx.draw()
  },



  onShareAppMessage: function (res) {
    return {
      title: '厚大计时器',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  }
})
